//
//  CollectViewController.m
//  News
//
//  Created by xalo on 16/3/10.
//  Copyright © 2016年 News. All rights reserved.
//

#import "CollectViewController.h"
#import "ShowViewController.h"
#import "FMDatabase+Database.h"
#import "NewsModel.h"
#import "VideoModel.h"
#import "DetialModel.h"
#import "PicListModel.h"
#import "UMSocial.h"


@interface CollectViewController ()


@property (strong, nonatomic) IBOutlet UIView *collectView;

@property (nonatomic,strong) NSMutableArray *IDArray;



@property (strong, nonatomic) IBOutlet UIView *clearView;


@property (nonatomic,strong) UIVisualEffectView *effectView;

@end

@implementation CollectViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectView.layer.cornerRadius = 8;
    self.collectView.layer.masksToBounds = YES;
    
    
    self.IDArray = [NSMutableArray array];
    
    
    [self showVisualView];
    [self.clearView addSubview:self.pictureView];
    
    
}




//设置毛玻璃效果
-(void)showVisualView
{
    

   self.effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)]];
    
    self.effectView.frame = [UIScreen mainScreen].bounds;
    self.effectView.alpha = 1;
   
    [self.pictureView addSubview:self.effectView];
    


}



- (BOOL)shouldAutorotate {
    return YES;
}



-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskPortrait;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)collectOrShare:(UIButton *)sender {
    
        if (sender.tag == 111) {
            
//            CollectViewController *collect = [[UIStoryboard storyboardWithName:@"Collect" bundle:nil] instantiateViewControllerWithIdentifier:@"collect"];
//            [collect.view removeFromSuperview];
//            [collect removeFromParentViewController];
//          
//            
//            
//            
//         
//                
//                
//                [UMSocialSnsService presentSnsIconSheetView:self
//                                                     appKey:@"56cd552467e58e0b4f001160"
//                                                  shareText:[NSString stringWithFormat:@"%@ %@",self.titShare , self.pictureLink]
//                                                 shareImage:[UIImage imageNamed:@"icon.png"]
//                                            shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToTencent,UMShareToQzone,UMShareToQQ,nil]
//                                                   delegate:nil];
//                
            
           
            

      [UMSocialSnsService presentSnsIconSheetView:self
                                                 appKey:@"56cd552467e58e0b4f001160"
                                              shareText:[NSString stringWithFormat:@"%@ %@",self.titShare , self.pictureLink]
                                             shareImage:[UIImage imageNamed:@"icon.png"]
                                        shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToTencent,UMShareToQzone,UMShareToQQ,nil]
                                               delegate:nil];
            
            

            
            
            
            
            
         
            
            
        ShowViewController *showVC = [[UIStoryboard storyboardWithName:@"Share" bundle:nil] instantiateViewControllerWithIdentifier:@"show"];
        
        showVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            
            showVC.link = self.pictureLink;//分享图片链接
            showVC.tit = self.titShare; // 标题
            
            showVC.pictureView = self.pictureView;
//            showVC.videoModel = self.videoModel;
//            showVC.tit = self.videoModel.title;
//            showVC.link = self.videoModel.url;

//            
            
        [self presentViewController:showVC animated:YES completion:nil];
        
            [self.view removeFromSuperview];
            [self removeFromParentViewController];

          
    }else if (sender.tag == 112){
  
           
        if (self.newsModel) {
            [self isCollected];
            
        }else if (self.videoModel){
            
            [self hasBeenCollected];
        }
        else {
            [self picCollected];
        }
      
        [UIView animateWithDuration:1.0 animations:^{
            
            [self.view removeFromSuperview];
            [self removeFromParentViewController];
            
        }];
    }
}


-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
//    [self dismissViewControllerAnimated:NO completion:nil];
    

}

#pragma mark 判断新闻是否已收藏
-(void)isCollected {
    
    NSArray *array = [[FMDatabase shareManager] selectCollectionByCategory:self.newsModel.category];
    
    for (NewsModel *model in array) {
        
        if ([self.newsModel.ID isEqualToString:model.ID]) {
            //弹出已经收藏
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"已经被收藏" preferredStyle:(UIAlertControllerStyleAlert)];
            
            [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:1];
            [self presentViewController:alert animated:YES completion:nil];
            
            [self.IDArray addObject:model.ID];
        }
    }
    if (![self.IDArray containsObject:self.newsModel.ID]) {
        //弹出收藏成功
        [[FMDatabase shareManager] saveCollectionModel:self.newsModel withID:self.newsModel.ID];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:1];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
}


-(void)picCollected {
    
    NSArray *array = [[FMDatabase shareManager] selectCollectionByCategory:@"Model"];
    
    for ( NSArray *table in array) {
        
         DetialModel *detial = table[0];
      
    DetialModel *model = self.picArray[0];
    
    
    if ([detial.alt isEqualToString:model.alt]) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"已经被收藏" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:1];
        [self presentViewController:alert animated:YES completion:nil];
        
        
         }
      
    }
    if (self.picArray.count >= 1) {
        
        [[FMDatabase shareManager] saveCollectionOtherModel:self.picArray withID:self.storageID];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:1];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    
}




#pragma mark 判断是否已经收藏,并弹出提示框
-(void)hasBeenCollected {

    NSArray *array = [[FMDatabase shareManager] selectCollectionByCategory:self.videoModel.category];
    
    for (VideoModel *model in array) {
        
        if ([self.videoModel.ID isEqualToString:model.ID]) {
            //弹出已经收藏
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"已经被收藏" preferredStyle:(UIAlertControllerStyleAlert)];
            
            [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:1];
            [self presentViewController:alert animated:YES completion:nil];
            
            [self.IDArray addObject:model.ID];
        }
    }
    if (![self.IDArray containsObject:self.videoModel.ID]) {
        //弹出收藏成功
        [[FMDatabase shareManager] collectVideModel:self.videoModel];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:1];
        [self presentViewController:alert animated:YES completion:nil];

    }
}

//提示框消失
-(void)dismissAlertView:(UIAlertController *)alert
{
    
    [alert dismissViewControllerAnimated:YES completion:nil];

}


@end
